//
//  feedback   feedback UserFeedBack.m
//  BCW
//
//  Created by Mac on 16/3/30.
//  Copyright © 2016年 mac. All rights reserved.
//

#import "FeedBackVC.h"
#import "HBWPlaceholderTextView.h"
#import <SVProgressHUD.h>
#import "UIBarButtonItem+CustomItem.h"
#import "HBWNetDataManager.h"

@interface FeedBackVC ()

@property (nonatomic, strong) HBWPlaceholderTextView *feedbackView;

@property (nonatomic, strong) UITextField   *contact;
@end

@implementation FeedBackVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"意见反馈";
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithImage:@"return" hightImage:@"return" target:self action:@selector(popViewController:)];
    self.view.backgroundColor = WhiteColor;
    [self setupSubview];
    // Do any additional setup after loading the view.
}

- (void)setupSubview{
    // 联系方式
    UITextField *contact = [[UITextField alloc] initWithFrame:CGRectMake(10, 70, self.view.width - 20, 30)];
    contact.placeholder = @"联系方式:手机/QQ/邮箱";
    contact.layer.borderColor = [UIColor grayColor].CGColor;
    contact.layer.borderWidth = 0.5;
    [self.view addSubview:contact];
    _contact = contact;
    
    // 反馈
    HBWPlaceholderTextView *feedText = [[HBWPlaceholderTextView alloc] initWithFrame:CGRectMake(contact.left, contact.bottom  +10, contact.width, 120)];
    feedText.placeholder = @"请填写您宝贵的意见，我们期待您的建议和意见";
    [self.view addSubview:feedText];
    _feedbackView = feedText;
    
    // commit btn
    UIButton *commit = [UIButton buttonWithType:UIButtonTypeCustom];
    commit.frame = CGRectMake(feedText.left, feedText.bottom + 30, feedText.width, 35);
    [commit setTitle:@"立即提交" forState:UIControlStateNormal];
    commit.backgroundColor = ThemeColor;
    [commit addTarget:self action:@selector(commitClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:commit];
    
    //
    // 友好提示
    UILabel *remmid = [[UILabel alloc] initWithFrame:CGRectMake(0, commit.bottom + 2, 100, 20)];
    remmid.text = @"如果您对我们的产品有任何意见和建议，欢迎反馈给我们!";
    remmid.textAlignment = 1;
    remmid.font = SystemFont(10);
    [remmid sizeToFit];
    remmid.centerX = commit.centerX;
    [self.view addSubview:remmid];
    
}

- (void)commitClick:(UIButton *)sender{
    
    if (_contact.text.length != 11) {
        [SVProgressHUD showInfoWithStatus:@"请输入11位手机号码"];
        return;
    }
    else if ([_feedbackView.text isEqualToString:@""]) {
        [SVProgressHUD showInfoWithStatus:@"请输入信息"];
        return;
    }
    
    [SVProgressHUD showWithStatus:@"正在提交..."];
    
    NSMutableDictionary *param = [[NSMutableDictionary alloc] initWithObjectsAndKeys:_contact.text, @"contact", _feedbackView.text, @"suggest", nil];
    
    [NetMgr feedbackWithParam:param Success:^(id responeseObj) {
//        NSLog(@"%@", responeseObj[@"code"]);
//        NSInteger code = [responeseObj[@"code"]
        if ([responeseObj[@"code"] compare:@0] == NSOrderedSame) {
            [self showSuccess];
        }
        else{
            [self showFailer];
        }
    } failuer:^(NSError *error) {
        [self showFailer];
    }];
}

- (void)showSuccess{
    [SVProgressHUD showSuccessWithStatus:@"提交成功,感谢您的反馈!"];
}

- (void)showFailer{
    [SVProgressHUD showErrorWithStatus:@"提交失败!请稍候重试"];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}

- (void)popViewController:(id)sender{
    [self.navigationController popViewControllerAnimated:YES];
}

@end
